<template>
<el-container  class="Home_container">
  <!-- 头 -->
  <el-header>
    <div class="box">
       <img src="@/assets/timo.jpg" alt="">
       后台中心
    </div>
    <!-- 退出 -->
    <el-button type="info" @click="logout">退出</el-button>
  </el-header>

  <el-container>
    <!-- 左边 -->
    <el-aside :width="iscollapse ? '60px' : '200px' " >
      <!-- 开始 -->
      <el-menu :default-active="getPath" router collapse-transition :collapse="iscollapse"  background-color="#545c64" text-color="#fff" active-text-color="#ffd04b" unique-opened>
             <!-- 折叠按钮 --> <div class="translate" @click="HandleOpen">|||</div>
       <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id + ''">
        <!-- 一级 -->
        <template slot="title">
          <i class="el-icon-location"></i>
          <span>{{item.authName}}</span>
        </template>
        <!-- 二级 -->
          <el-menu-item @click="savepath('/' + sub.path)" :index="'/' + sub.path" v-for="sub in item.children" :key="sub.path">
             <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{sub.authName}}</span>
             </template>
          </el-menu-item>
       </el-submenu>
      </el-menu>
    </el-aside>
    <!-- 主体 -->
    <el-main>
      <router-view></router-view>
    </el-main>
  </el-container>
</el-container>
</template>
<script>
export default {
  data () {
    return {
      menuList: [],
      // 是否折叠
      iscollapse: false,
      // 得到path
      getPath: ''
    }
  },
  created () {
    this.getMenuList()
    // 获取激活存储
    this.getPath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    //   退出
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 得到API
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      // console.log(res)
      if (res.meta.status !== 200) return this.$Message.error(res.meta.msg)
      // return this.$Message.success(res.meta.msg)
      this.menuList = res.data
    },
    // 是否折叠
    HandleOpen () {
      this.iscollapse = !this.iscollapse
      console.log(this.iscollapse)
    },
    // 存储active路径,保持连接的激活
    savepath (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.getPath = activePath
    }
  }
}
</script>
<style lang="less" scoped>
.Home_container {
    height: 100%;
}
.el-container {
  width:100%;
  height:100%;
   background-color: #eee;
}
.el-header {
  background-color: #2b4b6b;
  display: flex;
  justify-content: space-between;
  .box {
    width: 100px;
    height: 100px;
    img {
      width: 50%;
    }
  }
}
// 折叠
.translate {
  background-color: rgb(84, 92, 100);
  color:#fff;
  text-align: center;
}
// 穿透 header
& /deep/ .el-header {
  height: 150px;
}
</style>
